#define MAX_CCTV 10

//======================CCTV Variables

new
	Float:CCTV_Positions[MAX_CCTV][3]=
	{
		{201.4361, 1858.0842, 14.1406},
		{255.5510, 1880.5475, 14.4609},
		{237.9282, 1807.8387, 12.1190},
		{299.4411, 1816.5137, 7.0000},
		{275.5638, 1841.6835, 10.0000},
		{332.0320, 1837.6627, 10.0000},
		{295.6037, 1853.4476, 10.0000},
		{266.4689, 1853.2777, 12.7578},
		{262.7636, 1889.6595, 11.0781},
		{274.4807, 1883.5447, -22.4219}
	},
	Float:CCTV_LookPos[MAX_CCTV][3],

	Float:CCTV_Elevation[MAX_CCTV],
	Float:CCTV_Rotation[MAX_CCTV],

	CCTV_Timer,
	CurrentCCTV[MAX_PLAYERS char] = -1,
	Text:CameraControlText,
	Menu:CCTV_Menu;

GetPlayersInCCTV()
{
	new count;
	PlayerLoop(i)if(bPlayerGameSettings[i]&UsingBaseCam)count++;
	return count;
}

//======================Pickup/Menu Variables

new basecontrol,
	Menu:BaseControlMenu,
	bool:BaseDoorOpen,
	bool:ControlRoomLocked,
	BaseLockdown;

//======================Door Pickup Variables

new frontgateinner,frontgateouter,
	frontdoorouter,frontdoorinner,
	door1outer,door1inner,
	door2outer,door2inner,
	door3outer,door3inner,
	door4outer,door4inner,
	hqdoorouter,hqdoorinner,
	door5inner,door5middle,door5outer,
	door6outer,door6inner;

//======================Door Object Variables

new armygate,
	frontdoorL,frontdoorR,
	door1,door2,
	h1,h2,
	door3,door4,
	d1r,d1l,
	hqd1,vdb,vdt,hqd2,
	door5,door6;

//======================Door Close Callbacks [for the timers]

	forward CloseDoorSet(doorsetid);
	forward HackBaseGate(playerid);
	forward CheckKeysForCCTV(playerid);
	forward script_MAP02_LoadItems();





//=================================ArmyBase Control Menu========================
script_MAP02_MenuRow(playerid, row, Menu:pMenu)
{
	if(pMenu == BaseControlMenu)
	{
		switch(row)
		{
			case 0:
			{
				if(!BaseDoorOpen)
				{
					MoveDynamicObject(armygate, 96.886208, 1922.040039, 17.310095, 4.0);
					BaseDoorOpen = true;
					msg(playerid, YELLOW, "Door Opened");

					SetPlayerCameraPos(playerid, 105.886208, 1918.947876, 20.310095);
					SetPlayerCameraLookAt(playerid, 96.886208, 1918.947876, 17.310095);

					SetTimerEx("resetcamera", 2000, false, "d", playerid);

					ShowMenuForPlayer(BaseControlMenu, playerid);
				}
				else
				{
					MoveDynamicObject(armygate, 96.902634, 1918.947876, 17.330902, 4);
					BaseDoorOpen = false;
					msg(playerid, YELLOW, "Door Closed");

					SetPlayerCameraPos(playerid, 105.886208, 1918.947876, 20.310095);
					SetPlayerCameraLookAt(playerid, 96.886208, 1918.947876, 17.310095);

					SetTimerEx("resetcamera", 2000, false, "d", playerid);

					ShowMenuForPlayer(BaseControlMenu, playerid);
				}
			}
			case 1:
			{
				if(ControlRoomLocked)
				{
					ControlRoomLocked = false;
					msg(playerid, YELLOW, "ControlRoom Unlocked");
				}
				else if(!ControlRoomLocked)
				{
					ControlRoomLocked = true;
					msg(playerid, YELLOW, "ControlRoom Locked");
				}
				ShowMenuForPlayer(BaseControlMenu, playerid);
			}
			case 2:
			{
				if(BaseLockdown)
				{
					BaseLockdown = false;
					msg(playerid, YELLOW, "Base Lockdown Ended");
				}
				else if(!BaseLockdown)
				{
					BaseLockdown = true;
					msg(playerid, YELLOW, "Base Under Lockdown");
				}
				ShowMenuForPlayer(BaseControlMenu, playerid);
			}
			case 3:
			{
				TogglePlayerControllable(playerid, 0);
				ShowMenuForPlayer(CCTV_Menu, playerid);
			}
		}
	}
	if(pMenu == CCTV_Menu)
	{
	    if(row==10)ShowMenuForPlayer(BaseControlMenu, playerid);
	    else SetPlayerToCCTVCamera(playerid, row);
	}
	return 1;
}
public CheckKeysForCCTV()
	PlayerLoop(i)if(bPlayerGameSettings[i]&UsingBaseCam)CheckKeys(i);
CheckKeys(playerid)
{
	new
		keys,
		updown,
		leftright,
		tmpID = CurrentCCTV[playerid];

	GetPlayerKeys(playerid, keys, updown, leftright);
	
	if(tmpID>-1)
	{
		if(leftright == KEY_RIGHT)
	  	{
	  		if(keys == KEY_SPRINT)
			  	CCTV_Rotation[tmpID]-=5.0;
			else
				CCTV_Rotation[tmpID]-=3.0;

	  		if(CCTV_Rotation[tmpID] < 0.0)
			  	CCTV_Rotation[tmpID]=359.0;
		}
		if(leftright == KEY_LEFT)
		{
			if(keys == KEY_SPRINT)
				CCTV_Rotation[tmpID]+=5.0;
			else
				CCTV_Rotation[tmpID]+=3.0;

			if(CCTV_Rotation[tmpID] >= 360.0)
				CCTV_Rotation[tmpID]=0.0;
		}
		if(updown == KEY_UP)
		{
			if(CCTV_Elevation[tmpID]<40.0)
			{
				if(keys == KEY_SPRINT)
					CCTV_Elevation[tmpID]+=5.0;
				else
					CCTV_Elevation[tmpID]+=3.0;
			}
		}
		if(updown == KEY_DOWN)
		{
			if(CCTV_Elevation[tmpID]>-40.0)
			{
				if(keys == KEY_SPRINT)
					CCTV_Elevation[tmpID]-=5.0;
				else
					CCTV_Elevation[tmpID]-=3.0;
			}
		}
		if(keys == KEY_CROUCH)ExitCCTV(playerid);
		MovePlayerCCTV(playerid);
	}
}
stock MovePlayerCCTV(playerid)
{
	new tmpID = CurrentCCTV[playerid];
/*
	CCTV_LookPos[tmpID][0] = CCTV_Positions[tmpID][0] + (CCTV_Elevation[tmpID] * floatsin(-CCTV_Rotation[tmpID], degrees));
	CCTV_LookPos[tmpID][1] = CCTV_Positions[tmpID][1] + (CCTV_Elevation[tmpID] * floatcos(-CCTV_Rotation[tmpID], degrees));
*/
    CCTV_LookPos[tmpID][0] = CCTV_Positions[tmpID][0] + (floatsin(-CCTV_Rotation[tmpID], degrees) * floatcos(CCTV_Elevation[tmpID], degrees));
    CCTV_LookPos[tmpID][1] = CCTV_Positions[tmpID][1] + (floatcos(-CCTV_Rotation[tmpID], degrees) * floatcos(CCTV_Elevation[tmpID], degrees));
	CCTV_LookPos[tmpID][2] = CCTV_Positions[tmpID][2] + (floatsin(CCTV_Elevation[tmpID], degrees));

	SetPlayerCameraLookAt(playerid, CCTV_LookPos[tmpID][0], CCTV_LookPos[tmpID][1], CCTV_LookPos[tmpID][2]);
}
SetPlayerToCCTVCamera(playerid, CCTV)
{
	CurrentCCTV[playerid] = CCTV;
	TogglePlayerControllable(playerid, 0);

	SetPlayerPos			(playerid, CCTV_Positions[CCTV][0], CCTV_Positions[CCTV][1], (CCTV_Positions[CCTV][2]-50));
	SetPlayerCameraPos		(playerid, CCTV_Positions[CCTV][0], CCTV_Positions[CCTV][1], CCTV_Positions[CCTV][2]);
	SetPlayerCameraLookAt	(playerid, CCTV_Positions[CCTV][0], CCTV_Positions[CCTV][1], CCTV_Positions[CCTV][2]-10.0);

	CCTV_LookPos[CCTV][0]	= CCTV_Positions[CCTV][0];
	CCTV_LookPos[CCTV][1]	= CCTV_Positions[CCTV][1]+0.2;
	CCTV_LookPos[CCTV][2]	= CCTV_Positions[CCTV][2];
	CCTV_Elevation[CCTV]	= 12.5;
	CCTV_Rotation[CCTV]		= 0.0;
	
	if(GetPlayersInCCTV()==0)CCTV_Timer=SetTimer("CheckKeysForCCTV", 100, true);

	bitTrue(bPlayerGameSettings[playerid], UsingBaseCam);
	TextDrawShowForPlayer(playerid, CameraControlText);

	MovePlayerCCTV(playerid);

	return 1;
}
ExitCCTV(playerid)
{
	if(CurrentCCTV[playerid] > -1)
	{
		TogglePlayerControllable(playerid, 0);
		SetPlayerPos(playerid, 213.1613, 1822.1256, 6.4141);

		SetCameraBehindPlayer(playerid);
		TextDrawHideForPlayer(playerid, CameraControlText);
		CurrentCCTV[playerid] = -1;
		if(GetPlayersInCCTV()==0)KillTimer(CCTV_Timer);
		ShowMenuForPlayer(CCTV_Menu, playerid);
		return 1;
	}
	return 0;
}


public HackBaseGate(playerid)
{
	MoveDynamicObject(armygate, 96.886208, 1922.040039, 17.310095, 4.0);
	TeamMsgF(pTeam(playerid), BLUE, "%s has hacked the base gate", GetName(playerid));
	TeamMsg(poTeam(playerid), RED, "Enemies have breached the front gate");
	TogglePlayerControllable(playerid, true);
	ClearAnimations(playerid);
	GiveXP(playerid, 10, "hacking gate");
}

BaseDoorCheck(playerid, buttonid)
{
	if(buttonid == basecontrol)
	{
		ShowMenuForPlayer(BaseControlMenu, playerid);
		SetPlayerFacingAngle(playerid, 90.0);
		SetCameraBehindPlayer(playerid);
		TogglePlayerControllable(playerid, 0);
		HideMsgBox(playerid);
		return 1;
	}
	if( (buttonid == frontgateinner || buttonid == frontgateouter) && DMmode != DM_MODE_CTF)
	{
		if(pTeam(playerid) == 1)
		{
			TogglePlayerControllable(playerid, false);
			ApplyAnimation(playerid, "bomber", "bom_Plant", 1.0, 1, 0, 0, 0, 5000);
			SetTimerEx("HackBaseGate", 5000, false, "d", playerid);
		}
		else
		{
			MoveDynamicObject(armygate, 96.886208, 1922.040039, 17.310095, 4.0);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 0);
		}
		return 1;
	}
	if(BaseLockdown)msg(playerid, RED, "The base is under lockdown and all doors are locked, unlock it from the control room");
	else
	{
		if(buttonid == frontdoorouter || buttonid == frontdoorinner)
		{
			MoveDynamicObject(frontdoorL, 219.976547, 1875.288086, 13.938993, 1);
			MoveDynamicObject(frontdoorR, 207.855698, 1875.288086, 13.938993, 1);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 1);
		}
		if(buttonid == door1outer || buttonid == door1inner)
		{
			MoveDynamicObject(door1, 226.477859, 1870.359863, 11.010899, 1);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 2);
		}
		if(buttonid == door2outer || buttonid == door2inner)
		{
			MoveDynamicObject(door2, 238.381897, 1873.953857, 14.194216, 1);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 3);
		}
		if(buttonid == door3outer || buttonid == door3inner)
		{
			MoveDynamicObject(door3, 247.270325, 1842.033691, 11.706713, 1);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 4);
		}
		if(buttonid == door4outer || buttonid == door4inner)
		{
			MoveDynamicObject(door4, 249.386978, 1805.945679, 10.437963, 1);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 5);
		}
		if(buttonid == door5inner || buttonid == door5middle || buttonid == door5outer)
		{
			MoveDynamicObject(door5, 270.502502, 1864.034058, 8.741133, 1);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 6);
		}
		if(buttonid == door6inner || buttonid == door6outer)
		{
			MoveDynamicObject(door6, 264.048431, 1875.085449, 9.267685, 1);
			SetTimerEx("CloseDoorSet", 5000, false, "d", 7);
		}
		if(buttonid == hqdoorouter || buttonid == hqdoorinner)
		{
			if(!ControlRoomLocked)
			{
				MoveDynamicObject(d1r, 233.793884, 1827.063477, 7.097370, 1);
				MoveDynamicObject(d1l, 233.793884, 1818.413452, 7.097370, 1);
				MoveDynamicObject(vdb, 233.448166, 1823.095581, 13.175995, 1);
				MoveDynamicObject(vdt, 233.150253, 1822.738525, 10.296906, 1);
				MoveDynamicObject(hqd1, 233.448166, 1823.095581, 13.175995, 1);
				MoveDynamicObject(hqd2, 233.035461, 1822.756836, 5.130999, 1);
				SetTimerEx("CloseDoorSet", 5000, false, "d", 8);
			}
			else msg(playerid, RED, "This door is locked and can only be opened from the inside, you will have to find another way in");
		}
	}
	return 1;
}
public CloseDoorSet(doorsetid)
{
	if(doorsetid==0)MoveDynamicObject(armygate, 96.902634, 1918.947876, 17.330902, 4);
	if(doorsetid==1)
	{
		MoveDynamicObject(frontdoorL, 215.951553, 1875.288086, 13.938993, 1);
		MoveDynamicObject(frontdoorR, 211.855515, 1875.287354, 13.938993, 1);
	}
	if(doorsetid==2)MoveDynamicObject(door1, 226.493378, 1870.385986, 13.167704, 1);
	if(doorsetid==3)MoveDynamicObject(door2, 238.401688, 1873.974609, 12.144247, 1);
	if(doorsetid==4)MoveDynamicObject(door3, 247.275406, 1842.032104, 8.441137, 1);
	if(doorsetid==5)MoveDynamicObject(door4, 249.382538, 1805.930664, 7.337998, 1);
	if(doorsetid==6)MoveDynamicObject(door5, 274.200775, 1864.004150, 8.741133, 1);
	if(doorsetid==7)MoveDynamicObject(door6, 267.051788, 1875.100952, 9.267685, 1);
	if(doorsetid==8)
	{
		MoveDynamicObject(d1r, 233.793884, 1825.885498, 7.097370, 1);
		MoveDynamicObject(d1l, 233.793884, 1819.572388, 7.097370, 1);
		MoveDynamicObject(hqd1, 233.448166, 1823.095581, 9.175995, 1);
		MoveDynamicObject(vdb, 233.150253, 1822.738525, 6.146933, 1);
		MoveDynamicObject(vdt, 233.150253, 1822.738525, 7.921940, 1);
		MoveDynamicObject(hqd2, 233.035461, 1822.756836, 7.706009, 1);
	}
	if(doorsetid==9)
	{
		MoveDynamicObject(h1, 256.294373, 1845.986572, 9.408466, 1);
		MoveDynamicObject(h2, 256.834991, 1835.665405, 5.262501, 1);
	}
}
public script_MAP02_LoadItems()
{
	BaseControlMenu = CreateMenu("Control", 1, 50.0, 200.0, 150.0);
	AddMenuItem(BaseControlMenu, 0, "Open/Close Gate");
	AddMenuItem(BaseControlMenu, 0, "Lock Control Room");
	AddMenuItem(BaseControlMenu, 0, "Lockdown Base");
	AddMenuItem(BaseControlMenu, 0, "CCTV");

	basecontrol=CreateButton(213.1613, 1822.1256, 6.4141, "Press to access the base security mainframe", _, _, 1);

	CCTV_Menu = CreateMenu("Choose Camera:", 1, 200, 100, 220);
	AddMenuItem(CCTV_Menu, 0, "Entrance");
	AddMenuItem(CCTV_Menu, 0, "Storage Room");
	AddMenuItem(CCTV_Menu, 0, "Generator Room");
	AddMenuItem(CCTV_Menu, 0, "South Tunnel");
	AddMenuItem(CCTV_Menu, 0, "Labs 1");
	AddMenuItem(CCTV_Menu, 0, "Labs 2");
	AddMenuItem(CCTV_Menu, 0, "Labs 3");
	AddMenuItem(CCTV_Menu, 0, "Labs 4");
	AddMenuItem(CCTV_Menu, 0, "Shaft top");
	AddMenuItem(CCTV_Menu, 0, "Shaft bottom");
	AddMenuItem(CCTV_Menu, 0, "Back");

	CameraControlText = TextDrawCreate(160, 400, "~y~Keys:~n~Arrow-Keys: ~w~Move The Camera~n~~y~Sprint-Key: ~w~Speed Up~n~~y~Crouch-Key: ~w~Exit Camera");
	TextDrawLetterSize(CameraControlText, 0.4, 0.9);
	TextDrawSetShadow(CameraControlText, 0);
	TextDrawUseBox(CameraControlText, 1);
	TextDrawBoxColor(CameraControlText, 0x00000055);
	TextDrawTextSize(CameraControlText, 380, 400);

//=================================================================Door Controls
	frontgateinner=CreateButton(97.6948, 1918.5541, 18.1833, "Press to activate gate", DEATHMATCH_WORLD);
	frontgateouter=CreateButton(95.7846, 1918.5755, 18.1172, "Press to activate gate", DEATHMATCH_WORLD);

	frontdoorouter=CreateButton(210.2342, 1877.4778, 13.1406, "Press to activate door", DEATHMATCH_WORLD);
	frontdoorinner=CreateButton(210.0500, 1872.9613, 13.1406, "Press to activate door", DEATHMATCH_WORLD);

	door1outer=CreateButton(225.6992, 1870.9802, 13.7421, "Press to activate door", DEATHMATCH_WORLD);
	door1inner=CreateButton(229.3916, 1871.3225, 13.3464, "Press to activate door", DEATHMATCH_WORLD);

	door2inner=CreateButton(237.4928,1871.3110,11.4609, "Press to activate door", DEATHMATCH_WORLD);
	door2outer=CreateButton(239.3345,1870.4381,11.4609, "Press to activate door", DEATHMATCH_WORLD);

	door3outer=CreateButton(250.3074,1843.1713,8.7734, "Press to activate door", DEATHMATCH_WORLD);
	door3inner=CreateButton(250.0694,1840.6854,8.7578, "Press to activate door", DEATHMATCH_WORLD);

	door4outer=CreateButton(248.7670,1806.6218,7.5547, "Press to activate door", DEATHMATCH_WORLD);
	door4inner=CreateButton(249.7577,1805.1844,7.4213, "Press to activate door", DEATHMATCH_WORLD);

	hqdoorouter=CreateButton(234.1869,1821.3165,7.4141, "Press to activate door", DEATHMATCH_WORLD);
	hqdoorinner=CreateButton(228.3555,1820.2427,7.4141, "Press to activate door", DEATHMATCH_WORLD);

	door5outer=CreateButton(277.7430,1865.1031,8.7578, "Press to activate door", DEATHMATCH_WORLD);
	door5middle=CreateButton(271.00, 1862.00, 8.7578, "Press to activate door", DEATHMATCH_WORLD);
	door5inner=CreateButton(269.7875,1865.7642,8.7578, "Press to activate door", DEATHMATCH_WORLD);

	door6outer=CreateButton(269.4969,1873.1721,8.6094, "Press to activate door", DEATHMATCH_WORLD);
	door6inner=CreateButton(270.6281,1875.8774,8.4375, "Press to activate door", DEATHMATCH_WORLD);

//=========================================================================Doors
	armygate	= CreateDynamicObject(969, 96.902634, 1918.947876, 17.330902, 0.0000, 0.0000, 90.0000);
	frontdoorL	= CreateDynamicObject(2927, 215.951553, 1875.288086, 13.938993, 0.000000, 0.000000, 0.000000);
	frontdoorR	= CreateDynamicObject(2929, 211.855515, 1875.287354, 13.938993, 0.000000, 0.000000, 0.000000);
	door1		= CreateDynamicObject(1508, 226.493378, 1870.385986, 13.167704, 0.0000, 0.0000, 0.0000);
	door2		= CreateDynamicObject(1508, 238.401688, 1873.974609, 12.144247, 0.0000, 179.6226, 0.0000);
	h1			= CreateDynamicObject(1508, 256.294373, 1845.986572, 9.408466, 0.0000, 0.0000, 0.0000);
	h2			= CreateDynamicObject(1508, 256.834991, 1835.665405, 5.262501, 0.0000, 0.0000, 0.0000);
	door3		= CreateDynamicObject(1508, 247.275406, 1842.032104, 8.441137, 0.0000, 0.0000, 90.0000);
	door4		= CreateDynamicObject(1508, 249.382538, 1805.930664, 7.337998, 0.0000, 0.0000, 90.0000);
	d1r			= CreateDynamicObject(1508, 233.793884, 1825.885498, 7.097370, 0.0000, 0.0000, 0.0000);
	d1l			= CreateDynamicObject(1508, 233.793884, 1819.572388, 7.097370, 0.0000, 0.0000, 0.0000);
	hqd1		= CreateDynamicObject(1966, 233.448166, 1823.095581, 9.175995, 0.0000, 0.0000, 90.0000);
	vdb			= CreateDynamicObject(16637, 233.150253, 1822.738525, 6.146933, 0.0000, 0.0000, 0.0000);
	vdt			= CreateDynamicObject(16637, 233.150253, 1822.738525, 7.921940, 0.0000, 0.0000, 0.0000);
	hqd2		= CreateDynamicObject(18553, 233.035461, 1822.756836, 7.706009, 0.0000, 0.0000, 359.1406);
	door5		= CreateDynamicObject(1508, 274.200775, 1864.004150, 8.741133, 0.0000, 0.0000, 90.0000);
	door6		= CreateDynamicObject(1508, 267.051788, 1875.100952, 9.267685, 0.0000, 0.0000, 90.0000);
}
